package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.repository;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.gts.bpaas.framework.api.rest.dto.ResultResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.enums.StatusEnum;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.HttpClientUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.StringUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.common.ErrorCode;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.notice.FlowNoticeSettingsVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowNoticeSettingsRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.common.exception.BusiException;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowNoticeSettings;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.mapper.FlowNoticeSettingsDao;
import org.flowable.engine.common.impl.util.CollectionUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * (FlowNoticeSettings)表服务实现类
 *
 * @author makejava
 * @since 2021-04-27 18:57:40
 */
@Service("flowNoticeSettingsService")
public class FlowNoticeSettingsServiceImpl implements FlowNoticeSettingsRepository {
    @Resource
    private FlowNoticeSettingsDao flowNoticeSettingsDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public FlowNoticeSettingsVo queryById(Integer id) {
        FlowNoticeSettings flowNoticeSettings = this.flowNoticeSettingsDao.queryById(id);
        if (Objects.isNull(flowNoticeSettings)){
            return null;
        }
        FlowNoticeSettingsVo vo = new FlowNoticeSettingsVo();
        BeanUtils.copyProperties(flowNoticeSettings, vo);
        return vo;
    }

    /**
     * 查询多条数据
     * FlowNoticeSettingsVo flowNoticeSettingsVo
     *
     * @return 对象列表
     */
    @Override
    public PageInfo<FlowNoticeSettingsVo> queryAll(FlowNoticeSettingsVo flowNoticeSettingsVo) {
        Integer pageNum = 1;
        Integer pageSize = 10;
        if (Objects.nonNull(flowNoticeSettingsVo)) {
            pageNum = flowNoticeSettingsVo.getPageNum() == null ? 1 : flowNoticeSettingsVo.getPageNum();
            pageSize = flowNoticeSettingsVo.getPageSize() == null ? 10 : flowNoticeSettingsVo.getPageSize();
        }
        PageHelper.startPage(pageNum, pageSize);
        List<FlowNoticeSettings> flowNoticeSettings = this.flowNoticeSettingsDao.queryAll(flowNoticeSettingsVo);
        PageInfo<FlowNoticeSettings> pageInfo = new PageInfo<>(flowNoticeSettings);
        List<FlowNoticeSettingsVo> collect = flowNoticeSettings.stream().map(this::convert).collect(Collectors.toList());
        PageInfo<FlowNoticeSettingsVo> pageInfoVo = new PageInfo<>();
        BeanUtils.copyProperties(pageInfo, pageInfoVo);
        pageInfoVo.setList(collect);
        return pageInfoVo;
    }

    /**
     * 新增数据
     *
     * @param flowNoticeSettingsVo 实例对象
     * @return 实例对象
     */
    @Override
    public FlowNoticeSettingsVo insert(FlowNoticeSettingsVo flowNoticeSettingsVo) {
        //参数验证
        if (Objects.isNull(flowNoticeSettingsVo) || StringUtils.isEmpty(flowNoticeSettingsVo.getNoticeThemCode())) {
            throw new BusiException(ErrorCode.PARAMETER_ERROR);
        }
        this.checkNoticeSettingExist(flowNoticeSettingsVo);

        FlowNoticeSettings flowNoticeSettings =  this.convert(flowNoticeSettingsVo);
        this.flowNoticeSettingsDao.insert(flowNoticeSettings);
        return this.convert(flowNoticeSettings);
    }

    /**
     * 修改数据
     *
     * @param flowNoticeSettingsVo 实例对象
     * @return 实例对象
     */
    @Override
    public FlowNoticeSettingsVo update(FlowNoticeSettingsVo flowNoticeSettingsVo) {
        //参数验证
        if (flowNoticeSettingsVo.getId() == null) {
            throw new BusiException(ErrorCode.PARAMETER_ERROR);
        }
        //配置存在性验证
        FlowNoticeSettingsVo flowNoticeSettingsVo1 = this.queryById(flowNoticeSettingsVo.getId());
        if (Objects.isNull(flowNoticeSettingsVo1)){
            throw new BusiException(ErrorCode.NOTICE_SETTINGS_NOT_EXIST);
        }
        flowNoticeSettingsVo1.setNoticeThemCode(flowNoticeSettingsVo.getNoticeThemCode());
        flowNoticeSettingsVo1.setStatus(flowNoticeSettingsVo.getStatus());
        this.checkNoticeSettingExist(flowNoticeSettingsVo1);
        FlowNoticeSettings flowNoticeSettings = this.convert(flowNoticeSettingsVo);
        this.flowNoticeSettingsDao.update(flowNoticeSettings);
        return this.queryById(flowNoticeSettings.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.flowNoticeSettingsDao.deleteById(id) > 0;
    }

    @Override
    public int insertOrUpdate(FlowNoticeSettingsVo flowNoticeSettingsVo) {
        FlowNoticeSettings convert = this.convert(flowNoticeSettingsVo);
        return this.flowNoticeSettingsDao.insertOrUpdate(convert);
    }

    @Override
    public Object getTemplates() {
        String url = "https://api.gts.work/qingyun/mock/2746/openapi/notify/getTemplates";
        String s = HttpClientUtils.httpGet(url, new HashMap<>());
        return JSONObject.parseObject(s, ResultResponse.class).getData();

    }

    private FlowNoticeSettingsVo convert(FlowNoticeSettings flowNoticeSettings) {
        if (Objects.isNull(flowNoticeSettings)) {
            return null;
        }
        FlowNoticeSettingsVo vo = new FlowNoticeSettingsVo();
        BeanUtils.copyProperties(flowNoticeSettings, vo);
        return vo;
    }

    private FlowNoticeSettings convert(FlowNoticeSettingsVo flowNoticeSettingsVo) {
        if (Objects.isNull(flowNoticeSettingsVo)) {
            return null;
        }
        FlowNoticeSettings flowNoticeSettings = new FlowNoticeSettings();
        BeanUtils.copyProperties(flowNoticeSettingsVo, flowNoticeSettings);
        return flowNoticeSettings;
    }

    private void checkNoticeSettingExist(FlowNoticeSettingsVo flowNoticeSettingsVo){
        //存在启用状态的主题并且添加/更新的配置状态为启用状态时，提示该通知配置已存在
        FlowNoticeSettingsVo condition = new FlowNoticeSettingsVo();
        condition.setNoticeThemCode(flowNoticeSettingsVo.getNoticeThemCode());
        condition.setStatus(StatusEnum.ENABLE.getCode());
        List<FlowNoticeSettings> flowNoticeSettingsList = this.flowNoticeSettingsDao.queryAll(condition);
        if (CollectionUtil.isNotEmpty(flowNoticeSettingsList)&& StatusEnum.ENABLE.getCode().equals(flowNoticeSettingsVo.getStatus())) {
            throw new BusiException(ErrorCode.NOTICE_SETTINGS_EXIST);
        }
    }
}